//
//  XYTOrderListDetailView.m
//  XYTPayOrderSystem
//
//  Created by wupeng on 2017/8/27.
//  Copyright © 2017年 wupeng. All rights reserved.
//

#import "XYTOrderListDetailView.h"
#import "XYTDetailItemView.h"

#define CenterView_Width boundsWidth - 70.0f*2
#define CenterView_Height boundsHeight - 170.0f - 120.0f

@interface XYTOrderListDetailView ()


@end

@implementation XYTOrderListDetailView

- (instancetype)initWithFrame:(CGRect)frame withOrder:(XYTOrderModel *)model{
    if (self = [super initWithFrame:frame]) {
        self.model = model;
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    self.backgroundColor = [UIColor clearColor];
    //遮罩
    UIView *coverView = [[UIView alloc] initWithFrame:self.bounds];
    coverView.backgroundColor = [UIColor blackColor];
    coverView.alpha = 0.6;
    [self addSubview:coverView];
    //手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:[self class] action:@selector(hideDetail)];
    [coverView addGestureRecognizer:tapGesture];
    
    //中心view
    UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(70, 170, CenterView_Width, CenterView_Height)];
    centerView.layer.cornerRadius = 2.0;
    centerView.layer.masksToBounds = YES;
    centerView.backgroundColor = [UIColor whiteColor];
    [self addSubview:centerView];
    
    CGFloat detailItemHeight = (CenterView_Height*1.0f)/9;
    //订单号
    XYTDetailItemView *orderNumView = [[XYTDetailItemView alloc] initWithFrame:CGRectMake(0, 0, CenterView_Width, detailItemHeight)];
    orderNumView.titleLab.text = @"订单号";
    orderNumView.dataLab.text = _model.orderNo;
    [centerView addSubview:orderNumView];
    //订单金额
    XYTDetailItemView *orderAmountView = [[XYTDetailItemView alloc] initWithFrame:CGRectMake(0, detailItemHeight, CenterView_Width, detailItemHeight)];
    orderAmountView.titleLab.text = @"订单金额";
    orderAmountView.dataLab.text = FloatToStr(_model.moneyTotal);
    [centerView addSubview:orderAmountView];
    //现金
    XYTDetailItemView *cashNumView = [[XYTDetailItemView alloc] initWithFrame:CGRectMake(0, detailItemHeight*2, CenterView_Width, detailItemHeight)];
    cashNumView.titleLab.text = @"现金";
    cashNumView.dataLab.text = FloatToStr(_model.moneyCash);
    [centerView addSubview:cashNumView];
    //充值卡
    XYTDetailItemView *cardView = [[XYTDetailItemView alloc] initWithFrame:CGRectMake(0, detailItemHeight*3, CenterView_Width, detailItemHeight)];
    cardView.titleLab.text = @"充值卡";
    cardView.dataLab.text = FloatToStr(_model.moneyRcard);
    [centerView addSubview:cardView];
    //电子券
    XYTDetailItemView *eCardView = [[XYTDetailItemView alloc] initWithFrame:CGRectMake(0, detailItemHeight*4, CenterView_Width, detailItemHeight)];
    eCardView.titleLab.text = @"电子券";
    eCardView.dataLab.text = LongToStr(_model.moneyElectronic);
    [centerView addSubview:eCardView];
    //代金券
    XYTDetailItemView *voucherView = [[XYTDetailItemView alloc] initWithFrame:CGRectMake(0, detailItemHeight*5, CenterView_Width, detailItemHeight)];
    voucherView.titleLab.text = @"代金券";
    voucherView.dataLab.text = FloatToStr(_model.moneyVoucher);
    [centerView addSubview:voucherView];
    //费用 tudo:费用对应哪个字段？
    XYTDetailItemView *moneyTotalView = [[XYTDetailItemView alloc] initWithFrame:CGRectMake(0, detailItemHeight*6, CenterView_Width, detailItemHeight)];
    moneyTotalView.titleLab.text = @"费用";
    moneyTotalView.dataLab.text = FloatToStr(_model.moneyTotal);
    [centerView addSubview:moneyTotalView];
    //创建人
    XYTDetailItemView *creatUserView = [[XYTDetailItemView alloc] initWithFrame:CGRectMake(0, detailItemHeight*7, CenterView_Width, detailItemHeight)];
    creatUserView.titleLab.text = @"创建人";
    creatUserView.dataLab.text = _model.username;
    [centerView addSubview:creatUserView];
    //时间
    XYTDetailItemView *timeView = [[XYTDetailItemView alloc] initWithFrame:CGRectMake(0, detailItemHeight*8, CenterView_Width, detailItemHeight)];
    timeView.titleLab.text = @"时间";
    timeView.dataLab.text = _model.createDate;
    timeView.bottomLine.backgroundColor = [UIColor clearColor];//底部无线条
    [centerView addSubview:timeView];
    
}

+ (void)showDetail:(XYTOrderModel *)model{
    XYTOrderListDetailView *view = [[self alloc] initWithFrame:kScreen_Bounds withOrder:model];
    UIView *currentView = [self getCurrentViewController].view;
    [currentView addSubview:view];
}

+ (void)hideDetail{
    UIView *currentView = [self getCurrentViewController].view;
    
    XYTOrderListDetailView *detailView = [self getDetailView:currentView];
    if (detailView) {
        [detailView removeFromSuperview];
    }
}

+ (XYTOrderListDetailView *)getDetailView:(UIView *)view{
    NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
    for (UIView *subview in subviewsEnum) {
        if ([subview isKindOfClass:self]) {
            return (XYTOrderListDetailView *)subview;
        }
    }
    return nil;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
